package game.components;


import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Bacalá
 */
public class Balao {

    private Image imagem = null, explosao = null, planeDir = null, tempImg = null;
    private int maxX, maxY, x, y, xI, yI;
    private int passoX = 1, passoY = 3, pontos, derrubado = 0;
    private boolean visible = true;

    public Balao(int maxX, int maxY, int ptos) {
        this.maxX = maxX;
        this.maxY = maxY;
        this.pontos = ptos;
        try {
            imagem = Image.createImage("/images/planeEsq.png");
            planeDir = Image.createImage("/images/planeDir.png");
            explosao = Image.createImage("/images/byePlane.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public int getDerrubado() {
        return derrubado;
    }

    public void setDerrubado(int derrubado) {
        this.derrubado = derrubado;
    }

    public void setXI(int xI) {
        this.xI = xI;
        this.setX(xI);
    }

    public void setYI(int yI) {
        this.yI = yI;
        this.setY(yI);
    }

    public void mexe() {
        if (visible) {
            if (x > this.maxX || x < 0) {
                passoX = -passoX;
                this.invertImg();
            }
            x += passoX;
        } else if (y < maxY && x < maxX && x > 0) {
            x += 2 + passoX;
            //inverteX();
            y += 2 * passoY;
        } else {
            visible();
            this.setX(xI);
            this.setY(yI);
        }
    }
    
    public void invertImg(){
        tempImg = imagem;
        imagem = planeDir;
        planeDir = tempImg;
    }

    public int getPassoX() {
        return passoX;
    }

    public void setPassoX(int passoX) {
        this.passoX = passoX;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Image getImagem() {
        return imagem;
    }

    public int getWidth() {
        return imagem.getWidth();
    }

    public int getHeight() {
        return imagem.getHeight();
    }

    public boolean testaColisao(Tiro b) {
        if (b.isVisible()) {
            if ( b.getMaxY() >= this.getMinY() &&
                 b.getMinY() <= this.getMaxY()       ) {
                if (b.getMinX() <= this.getMaxX() &&
                    b.getMaxX() >= this.getMinX()   ) {
                    b.acertou(this);
                    b.invisible();
                    b.setY(maxY - 25);
                    this.invisible();
                    this.derrubado++;
                    return true;
                }
            }
        }
        return false;
    }

    public void paint(Graphics g) {
        if (visible) {
            g.drawImage(this.getImagem(), this.getX(), this.getY(), Graphics.HCENTER | Graphics.VCENTER);
        } else {
            g.drawImage(explosao, this.getX(), this.getY(), Graphics.HCENTER | Graphics.VCENTER);
        }
    }

    public int getMinX() {
        return this.x - this.getWidth() / 3;
    }

    public int getMaxX() {
        return this.x + this.getWidth() / 3;
    }

    public int getMinY() {
        return this.y - this.getHeight() / 3;
    }

    public int getMaxY() {
        return this.y + this.getHeight() / 3;
    }

    public void inverteX() {
        passoX = -passoX;
    }

    public void invisible() {
        this.visible = false;
    }

    public void visible() {
        this.visible = true;
    }

    public int getPontos() {
        return pontos;
    }


}
